puts "Match!" if /abc/.match("The alphabet starts with abc.")
puts "Match!" if "The alphabet starts with abc.".match(/abc/)
puts "Match!" if /abc/ =~ "The alphabet starts with abc."
puts "Match!" if "The alphabet starts with abc." =~ /abc/

puts "The alphabet starts with abc" =~ /abc/
puts /abc/.match("The alphabet starts with abc.")

puts /([A-Za-z]+),[A-Za-z]+,(Mrs?\.)/.match("Peel,Emma,Mrs.,talented amateur")
puts "Dear #{$2} #{$1}"

string = "My phone number is (123) 555-1234."
phone_re = /\((\d{3})\)\s+(\d{3})-(\d{4})/
m = phone_re.match(string)
unless m
	puts "There was no match-sorry"
    exit
end
print "The whole string we started with. "
puts m.string
print "The entire part of the string that matched"
puts m[0]
puts "The three captures:"
3.times do |index|
	puts "Capture ##{index+1}: #{m.captures[index]}"
end
puts "Here's another way to get at the first capture:"
print "Capture #1:"
puts m[1]

print "The part of the string before the part that matched was:"
puts m.pre_match
print "The part of the string after the part that matched was:"
puts m.post_match
print "The second capture began at character "
puts m.begin(2)
print "The third capture ended at character "
puts m.end(3)
# 贪婪
string = "abc!def!ghi!"
match = /.+!/.match(string)
puts match[0]
match = /.+?!/.match(string)
puts match[0]

# 锚点
comment_regexp = /^\s*#/
match = comment_regexp.match("  # Pure comment!")
puts "-----------------"
if match
	puts match[0]
end
match = comment_regexp.match(" x = 1 # Code plus comment!")
puts "-----------------"
if match
	puts match[0]
end
# 先行断言 匹配但不被消费，正向?=,负向?!
str = "123 456. 789"
m = /\d+(?=\.)/.match(str)
puts m[0]
# 回顾断言 正向?<=，负向?<!
re = /(?<=David )Black/
str = 'David Black'
m = re.match(str)
if m
	puts m[0]
end
# 条件匹配
re = /(a)?(?(1)b|c)/
m = re.match("ab")
puts m[0]

m = re.match("c")
if m
	puts m[0]
end

p "testing 1 2 3 testing 4 5 6".scan(/\d/)
str = "Leopold Auer was the teacher of Jascha Heifetz"

violinists = str.scan(/([A-Z]\w+)\s+([A-Z]\w+)/)
p violinists

line = "first_name=david;last_name=black;counntry=usa"
record = line.split(/=|;/)
p record
v = "capitalize the first vowel"
v.sub!(/[aeiou]/) {|s| s.upcase}
puts v

p ["USA","UK","France","Germany"].grep(/[a-z]/)
p "aDvid".sub(/([a-z])([A-Z])/,'\2\1')

p [1,2,3].grep(/1/)